﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace TorrentLaunch
{
    public partial class ConfigForm : Form
    {
        public Configuration Config = new Configuration();// { get; set; }

        public ConfigForm()
        {
            InitializeComponent();
            AcceptButton = okButton;
            CancelButton = cancelButton;
            
            Shown += new EventHandler(ConfigForm_Shown);
        }

        void  ConfigForm_Shown(object sender, EventArgs e)
        {
            torrentClient.Text = Config.localClientExe;
            nmtDir.Text = Config.nmtWatchPath;
        }

        private void browseClient_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = Config.localClientExe;
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                Config.localClientExe = openFileDialog.FileName;
                torrentClient.Text = Config.localClientExe;
            }
        }

        private void nmtBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.SelectedPath = "";
            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                Config.nmtWatchPath = folderBrowserDialog.SelectedPath;
                nmtDir.Text = Config.nmtWatchPath;
            }
        }

        private void torrentClient_TextChanged(object sender, EventArgs e)
        {
            Config.localClientExe = torrentClient.Text;
        }

        private void nmtDir_TextChanged(object sender, EventArgs e)
        {
            Config.nmtWatchPath = nmtDir.Text;
        }

    }

    public class Configuration
    {
        public string localClientExe = "C:\\Program Files\\uTorrent\\uTorrent.exe";
        public string nmtWatchPath = "\\\\pch-a110\\share\\Apps\\cTorrent\\watch";

        public bool Load()
        {
            // Attempt to open the key
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\TorrentLaunch");

            // If the return value is null, the key doesn't exist
            if (key == null)
            {
                return false;
            }

            localClientExe = (string)key.GetValue("LocalClient", null);
            nmtWatchPath = (string)key.GetValue("NMTWatchPath", nmtWatchPath);
            key.Close();

            return true;
        }
        public void Save()
        {
            // Attempt to open the key
            RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\TorrentLaunch");

            // Attempt to retrieve the value X; if null is returned, the value
            // doesn't exist in the registry.
            key.SetValue("LocalClient", localClientExe);
            key.SetValue("NMTWatchPath", nmtWatchPath);
            key.Close();
        }
    }
}
